package com.market.controller.admin;

import com.market.bean.MarketBrand;
import com.market.bean.bo.admin.AdminBrandUpdateBo;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.BaseRespVo;
import com.market.bean.common.CommonData;
import com.market.bean.validation.ValidationBrand;
import com.market.bean.vo.admin.AdminBrandCreateVo;
import com.market.exception.ValidationException;
import com.market.service.admin.AdminBrandService;
import com.market.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @Author:WalterChan
 * @Decription:AdminBrandController
 * @Date Created in 2022-12-29-21:52
 */

@RestController
@RequestMapping("admin/brand")
public class AdminBrandController {
    @Autowired
    AdminBrandService adminBrandService;



//    http://182.92.235.201:8083/admin/brand/list?page=1&limit=20&sort=add_time&order=desc
//    http://182.92.235.201:8083/admin/brand/list?page=1&limit=20&id=1046056&sort=add_time&order=desc
//    http://182.92.235.201:8083/admin/brand/list?page=1&limit=20&id=&name=22&sort=add_time&order=desc
//    http://182.92.235.201:8083/admin/brand/list?page=1&limit=20&id=123&name=22&sort=add_time&order=desc
//    http://182.92.235.201:8083/admin/brand/list?page=1&limit=40&id=&name=&sort=add_time&order=desc
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo info,String id,String name){

        CommonData<MarketBrand> data= adminBrandService.list(info,id,name);

        return BaseRespVo.ok(data);

    }
//Request URL: http://182.92.235.201:8083/admin/brand/create
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody @Valid ValidationBrand brand, BindingResult bindingResult  ) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        BaseRespVo msg = ValidationUtil.getBaseRespVo(bindingResult);
        if (msg!=null) return msg;

        AdminBrandCreateVo data= adminBrandService.create(brand);

        return BaseRespVo.ok(data);

    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody @Valid AdminBrandUpdateBo updateBo,BindingResult bindingResult ) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        BaseRespVo msg = ValidationUtil.getBaseRespVo(bindingResult);
        if (msg!=null) return msg;

        AdminBrandCreateVo data= adminBrandService.update(updateBo);
        return BaseRespVo.ok(data);

    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketBrand marketBrand){
        adminBrandService.delete(marketBrand);
        return BaseRespVo.ok();

    }
}
